package com.sfac.javaSpringBoot.modules.account.dao;

import com.sfac.javaSpringBoot.modules.account.entity.Resource;
import com.sfac.javaSpringBoot.modules.common.entity.SearchBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface ResourceRepository extends JpaRepository<Resource,Integer>, JpaSpecificationExecutor<Resource> {
    //    多条数据插入
    @Query(nativeQuery=true,value="insert into account(create_date,permit)" +
            "values (:#{#resource.createDate},:#{#resource.permit})")
    @Modifying
    void insertResource(@Param("resource") Resource resource);

    @Select("<script>"
            + "select * from account_resource "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (permission like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Resource> getResourcesBySearchBean(SearchBean searchBean);
}
